var app = getApp();
var start = {};
Page({
    data:{
        siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
        curPage : "/zofui_sales/vedio/index",
        datalist : [],
        inited : false,
        current : 0,
        goods : {},
        select : false,
        prenum : 0,
        tuijian : [],
        page : {
            isend : false,
            doing : false,
            doo : 'vedio',
            pdata : {
                op : 'list',
                page : 1,
                otype : 0,
                vtype : 1,
                num : 6,
                gid : 0,
                filgid : 0,
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        iconPlayShow: true,
        VideoContext : false,
        playing : false,
        translateY : 0,
        curVideoIndex : 0,
        isClearSwipeTip : false,
		
    },
    onLoad:function(options){
        var self = this;
        var zfid = options.zfid ? options.zfid : '';
        var gid = options.gid ? options.gid : 0;
        var pages = getCurrentPages();

        app.com.getLocation(self,function(loc){
            self.setData({
                'page.pdata.lat' : loc.latitude,
                'page.pdata.lng' : loc.longitude,
                'page.pdata.gid' : gid,
                'page.pdata.zfid' : zfid,
            });
            app.com.auth(function(user){
                app.com.getPage(self,self.data.page,20,function(res){
                    var data = res.data.data;
                    self.setData({
                       datalist : data.list,
                       params : data.params,
                       sharedata : data.sharedata,
                       haveback : pages.length >= 2 ? true : false,
                       'page.pdata.gid' : 0,
                       inited : true,
                    },function(){
                        var e = {
                            detail : {
                                current : 0
                            }
                        }
                        self.onChange(e);
                    });

                },false,function(res){
                    app.com.alert(res.data.message,function(){
                        app.com.backPage(zfid);
                    });
                });
            });
        });
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.goods.title,
            path: '/zofui_sales/vedio/index?zfid='+self.data.params.zfid+'&gid='+self.data.goods.id,
            imageUrl : self.data.goods.thumb,
        }
    },
    onShareTimeline(){
        return {
            title: this.data.goods.title,
            query: 'zfid='+this.data.params.zfid+'&gid='+this.data.goods.id,
            imageUrl : this.data.goods.thumb,
        }
    },
    showtuijian(){
        this.setData({
            select : !this.data.select
        })
    },
    changeSubject(type){
        var self = this;
        var postdata = {
            op:type,
            logid:self.data.goods.logid ? self.data.goods.logid  : 0,
        }
        app.com.http('vedio','POST',postdata,0,false,'','',function(res){
            if (!res.data.errno && res.data) {
                self.setData({
                    goods : res.data.data.goods,
                    tuijian : res.data.data.tuijian,
                })
            }else{
                app.com.toast(res.data.message);
            }
            self.data.ising = false;
        })
    },
    playvideo(){
        if( this.data.VideoContext ){
            var video = this.data.VideoContext;
        }else{
            var video = this.data.VideoContext = wx.createVideoContext('myvideo');
        }
        video.play()
        this.setData({
            iconPlayShow : false,
            playing : true,
        })
        
    },
    pauseVideo() { //暂停\播放
        if( this.data.VideoContext ){
            var video = this.data.VideoContext;
        }else{
            return false;
        }
        if (this.data.playing) {
            video.pause()
            var iconPlayShow = true
        } else {
            video.play()
            var iconPlayShow = false
        }
        this.setData({
            iconPlayShow : iconPlayShow,
            playing : !this.data.playing,
        })
        
    },
    onChange(e){
        var self = this;
        if( this.data.changeing ) return false;
        this.data.changeing = true;

        let current = e.detail.current;

        this.setData({
            iconPlayShow : false,
            playing : true,
            curVideoIndex : current,
            goods : this.data.datalist[current]
        })

        if(current >= this.data.datalist.length-1){
            this.morelist();
            this.setData({
                showLoading : true
            })
        }

        if( this.data.VideoContext ){
            this.data.VideoContext.stop();
        }
        this.data.VideoContext = wx.createVideoContext('vinfo'+current);
        this.data.VideoContext.play();

        this.data.changeing = false;

        this.readlog();
    },
    readlog(){
        var self = this;
        var postdata = {
            op : 'readlog',
            gid : this.data.goods.id,
        }
        app.com.http('vedio','POST',postdata,0,false,'','',function(res){
            self.data.changeing = false;
        });
    },
    morelist : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list),
                showLoading : false,
            });
        });
    },
    back(){
        wx.navigateBack({
            fail : function(){
                wx.switchTab({
                  url: '/zofui_sales/pages/index/index'
                })
            }
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    }
})